package com.etrans.web.gps.business.report.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.report.domain.EnterpriseRemindVo;



/** 
* @author 作者  jia: 
* @version 创建时间：2018年1月23日 下午5:15:33 
* 类说明 
*/
@Service
public class EnterpriseRemindService extends BaseService {
    
    
    /*
     * 查找司机或者企业的名字
     * flag 参数 0代表企业 1代表司机
     */
	public List<HashMap<String, Object>> queryDriverOrEnterpriseName(String flag,String name) {
		Map<String, Object> params = new HashMap<>();
		params.put("name", name);
		params.put("selected","selected");
		List list = null;
		if (flag.equals("1")) { //代表的是企业
			 list = (List) this.selectList("enterpriseQuery.findEnterpriseName", params);
		}else if (flag.equals("2")){ //代表的是司机
            list = (List) this.selectList("driverQuery.findDriverName", params);
		}
		return list;
	}
	
	/**
	 * 
	 * @param notifyObj
	 * @param notifyType
	 * @param objName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	//根据通知类型/对象/名字/时间段查询
	@SuppressWarnings("unchecked")
	public List<EnterpriseRemindVo> findByCindiction(Map<String, Object> params) {
		List<EnterpriseRemindVo> enterpriseRemindVos = new ArrayList<>();
		/*Map<String,Object> params = new HashMap<String,Object>();
		params.put("notifyObj", notifyObj);
		params.put("notifyType", notifyType);
		params.put("startTime", startTime);
		params.put("endTime",endTime);
		params.put("objId", objId);*/
		if (params.get("notifyObj").equals("1")) { //说明是企业
              enterpriseRemindVos = (List) this.selectList("enterpriseRemindQuery.queryEnterpriseByCondiction", params);
              
		}
	    if (params.get("notifyObj").equals("2")) { //说明是司机
			enterpriseRemindVos = (List) this.selectList("enterpriseRemindQuery.queryDriverByCondiction", params);
		}
		return enterpriseRemindVos;
	}
	
	public Map<Object,Object> loadChartData(Map<String, Object> params) {
		Map<Object, Object> result = new HashMap<>();
		List<String> xAxis = new ArrayList<>();
		List<Integer> ySMS = new ArrayList<>(); //短信的数量
		List<Integer> yMail = new ArrayList<>();//邮件的数量
		List<Integer> yphone = new ArrayList<>();//电话的数量
		List<EnterpriseRemindVo> enterpriseRemindVos = findByCindiction(params);
		if (enterpriseRemindVos == null || enterpriseRemindVos.size() == 0) {
			return null;
		}
		//处理数据
		for (EnterpriseRemindVo eVo:enterpriseRemindVos){
			if (xAxis.contains(eVo.getName())) {
				//获取对象的位置
				int index =xAxis.indexOf(eVo.getName());
				if (eVo.getNotifyType() == 1) {
					int i = yphone.get(index);
				    yphone.set(index, i+1);
				}else if (eVo.getNotifyType() == 2) {
					int a = yMail.get(index);
					yMail.set(index, a+1);					
				}else {
					int b =ySMS.get(index);
					ySMS.set(index, b+1);
				}
			}else {
				xAxis.add(eVo.getName());
				if (eVo.getNotifyType() == 1) {
					yphone.add(1);
					//其他的设置为0，确保其位置为某一对象
					yMail.add(0);
					ySMS.add(0); 
				}else if (eVo.getNotifyType() == 2) {
					yMail.add(1);
					yphone.add(0);
					ySMS.add(0);
				}else {
					ySMS.add(1);
					yphone.add(0);
					yMail.add(0);
				}
			}
			result.put("xAxis", xAxis);
			result.put("yPhone",yphone);
			result.put("yMail", yMail);
			result.put("ySMS",ySMS);
	}
		return result;	
  }
	//加载表格的内容
	public List<EnterpriseRemindVo> loadTableData(Map<String, Object> params) {
		//只需查询全部即可
	   	return findByCindiction(params);
	}
	
	public int  countTableData(Map<String, Object> params) {
		
		int count =  this.selectOne("enterpriseRemindQuery.count", params);
		return count;
	}
}
